package mianshi.lanxin;

import java.util.*;

public class T1 {

    public static void main(String[] args) {
        int[] da = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
        int[][] res = groupValue(2, da);
        for (int i = 0; i < res.length; i++) {
            for (int j = 0; j < res[i].length; j++) {
                System.out.print(res[i][j] + "    ");
            }
            System.out.println();
        }
    }


    public static int[][] groupValue(int maxCnt, int[] nums) {
        Arrays.sort(nums);
        int p = nums.length - 1;
        int q = nums.length - 1;
        List<List<Integer>> list = new ArrayList<>();
        while (q >= 0) {
            while (q >= 0 && nums[q] == nums[p]) {
                q--;
            }

            int count = (p - q) % maxCnt == 0 ? (p - q) / 2 : (p - q) / 2 + 1;
            for (int i = 0; i < count; i++) {
                int index = (q + 1) + i * maxCnt;
                int j = 0;
                ArrayList<Integer> t = new ArrayList<>();
                while (j < maxCnt && index <= p) {
                    t.add(nums[index++]);
                    j++;
                }
                list.add(t);
            }
            p = q;

        }
        System.out.println(list);
        List<int[]> res1 = new ArrayList<>();
        for (List<Integer> integers : list) {
            res1.add(integers.stream().mapToInt(Integer::intValue).toArray());
        }
        return res1.toArray(new int[][]{});

    }
}
